package com.ysd.dao.impl;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.User;
import com.ysd.util.DBManager;

public class UserDaoImpl {
	
	public int addMessage(User user ) {
		String sql = "insert into user (id,username,phone,email,content)values(null,'"+user.getUsername()+"','"+user.getPhone()+"','"+user.getEmail()+"','"+user.getContent()+"') ";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
		//从数据库中查询
	}
	
	/**
	 * 修改留言状态为已读
	 * @param user
	 * @return
	 */
	public int updateMessageById(User user ) {
		return DBManager.executeUpdate("update user set status='"+user.getStatus()+"' where id="+user.getId());
		//从数据库中查询
	}
	
	/**
	 * 修改留言状态为已回复
	 * @param user
	 * @return
	 */
	public int updateMessageReplyById(User user ) {
		return DBManager.executeUpdate("update user set status='已回复',reply='"+user.getReply()+"' where id="+user.getId());
		//从数据库中查询
	}
	
	/**
	 * 根据id删除
	 * @param mId
	 * @return 受影响行数
	 */
	public int delMessageById(int Id) {
		return DBManager.executeUpdate("DELETE FROM user where id=" + Id);
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return 多个id为String
	 */
	public int delUserByIds(String ids) {
		String sql = "delete from user where id in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 *  留言按照状态查询所有(分页查询)
	 * @param status
	 * @return
	 */
	public List<User> getUserByStatus(String status, int page, int size){
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1";
			if (status!=null && !status.equals("")) {
				sql += " and status='"+status+"'";
			}
			sql += " order by time desc";
			sql += " limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				 int id = rs.getInt("id");
				 String username = rs.getString("username");
				 String phone = rs.getString("phone");
				 String email = rs.getString("email");
				 String content = rs.getString("content");
				 String sta = rs.getString("status");
				 String reply = rs.getString("reply");
				 String time = rs.getString("time");
				 User user = new User(id, username, phone, email, content, sta, reply,time);
				 list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 *  留言按照状态查询所有(留言数量)
	 * @param status
	 * @return
	 */
	public List<User> getUserByStatusNum(String status){
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1";
			if (status!=null && !status.equals("")) {
				sql += " and status='"+status+"'";
			}
			sql += " order by time desc";
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				 int id = rs.getInt("id");
				 String username = rs.getString("username");
				 String phone = rs.getString("phone");
				 String email = rs.getString("email");
				 String content = rs.getString("content");
				 String sta = rs.getString("status");
				 String reply = rs.getString("reply");
				 String time = rs.getString("time");
				 User user = new User(id, username, phone, email, content, sta, reply, time);
				 list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 *  留言按照id查询单条数据
	 * @param Id
	 * @return
	 */
	public User getUserById(int Id){
		User user = null;
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from user where id="+Id;
			
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				 int id = rs.getInt("id");
				 String username = rs.getString("username");
				 String phone = rs.getString("phone");
				 String email = rs.getString("email");
				 String content = rs.getString("content");
				 String sta = rs.getString("status");
				 String reply = rs.getString("reply");
				 String time = rs.getString("time");
				 user = new User(id, username, phone, email, content, sta, reply, time);
			}
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	 
	
}

